package com.team5101.client.handler;


import com.team5101.model.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

public class AccountClientHandler extends ChannelInboundHandlerAdapter implements Callable {

	private ChannelHandlerContext channelHandlerContext;

	private RpcRequest para;

	private Object result;

	@Override
	public synchronized void channelActive(ChannelHandlerContext ctx) throws Exception {
		this.channelHandlerContext = ctx;
	}

	/**
	 * 写出数据，等待唤醒
	 *
	 * @return
	 * @throws Exception
	 */
	@Override
	public synchronized Object call() throws Exception {
		channelHandlerContext.writeAndFlush(this.para);
		wait();
		return result;
	}


	/**
	 * 收到服务器端低响应,唤醒等待线程
	 *
	 * @param ctx
	 * @param msg
	 * @throws Exception
	 */
	@Override
	public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		this.result = msg;
		notify();
	}

	/**
	 * 设置参数
	 *
	 * @param para
	 */
	public synchronized void setPara(RpcRequest para) {
		this.para = para;
	}

}
